//
//  SuperMarketViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/19.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "SuperMarketViewModel.h"
#import "GoodsTopSelectView.h"
#import "ShopCarView.h"
#import "AdressAndSortingView.h"
#import "ScreeningView.h"
#import "SupermarketStellTableViewCell.h"
#import "TableViewPleaseholdView.h"

@interface SuperMarketViewModel ()<UITableViewDelegate,UITableViewDataSource,GoodsTopDelegate>{
    AdressAndSortingView * screeningView;
    ScreeningView * screenView;
}
@property (nonatomic,strong) UIView * selfView;
@property (nonatomic,strong) UITableView * myTableView;
@property (nonatomic,strong) NSMutableArray * stellDataSource;
@end

@implementation SuperMarketViewModel

-(instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        self.myTableView = tableView;
        self.selfView = superView;
        self.stellDataSource = [NSMutableArray arrayWithCapacity:0];
        tableView.y = tableView.y + kHeightProportion_In_iPhoneTriploid(34);
        tableView.height = tableView.height - kHeightProportion_In_iPhoneTriploid(34);
        tableView.delegate = self;
        tableView.dataSource = self;
        if (self.accountType == AccountType_Ordinary) {
            tableView.rowHeight = kHeightProportion_In_iPhoneTriploid(132);
        }else{
            tableView.rowHeight = kHeightProportion_In_iPhoneTriploid(136);
        }
        tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(10))];
        [superView addSubview:tableView];
        //
        GoodsTopSelectView * topView = [[GoodsTopSelectView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(32)) inSuperView:superView];
        topView.delegate = self;
        if (self.accountType == AccountType_Ordinary) {
            [self shopCarView];
        }
        [self supermarketStellList];
    }
    return self;
}

-(void)shopCarView{
    //
    ShopCarView * shopView = [[ShopCarView alloc] initWithInSuperView:self.selfView];
    [shopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.selfView.mas_right).offset(-kWidthProportion_In_iPhoneTriploid(15));
        make.bottom.equalTo(self.myTableView.mas_bottom).offset(-kHeightProportion_In_iPhoneTriploid(35));
        make.width.height.equalTo(@75);
    }];
}

-(void)noDataShowView{
    if (self.stellDataSource.count == 0) {
        TableViewPleaseholdView * nodata_View = [[TableViewPleaseholdView alloc] initWithFrame:CGRectMake(0, kHeightProportion_In_iPhoneTriploid(130), KScreenWidth, kHeightProportion_In_iPhoneTriploid(200)) thePicStr:@"icon_quesehng_shangjiaeuzhu" theTosStr:@"暂无内容～" theMiaoStr:@"" theButStr:@"" theButWidth:0];
        [self showNoDataImageView:nodata_View];
    }
}

#pragma mark -- g钢材超市
-(void)supermarketStellList{
    @weakify(self)
    _market_recommendCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self)
        RACSignal *requstSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            NSDictionary * adreeDic = @{@"categoryId":@"",
                                        @"productIds":@"",
                                        @"specificationsIds":@"",
                                        @"materialIds":@"",
                                        @"factoryId":@"",
                                        @"tradingMode":@"",
                                        @"priceFlag":@"",//1:升序；2：降序
                                        @"timeFlag":@"",//上架时间,1:升序；2：降序
                                        @"cityCode":@"",
                                        @"page":@([input integerValue]),
                                        @"pageSize":@15};
            if ([input integerValue] == 1) {
                [self.stellDataSource removeAllObjects];
            }
            NSString * requestStr;
            if (self.accountType == AccountType_Ordinary) {
                requestStr = kMallProductList;
            }else{
                requestStr = kCommoditystoreProductinfos;
            }
            [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,requestStr] lodingView:nil lodingTextString:@"" isLoding:YES parameters:adreeDic success:^(id  _Nullable response) {
                [subscriber sendNext:response];
                [subscriber sendCompleted];
            } failure:^(NSError * _Nullable error) {
                [self noDataShowView];
                [subscriber sendError:error];
                [subscriber sendCompleted];
            }];
            return nil;
        }];
        return [requstSignal map:^id(NSDictionary *value) {
            SupermarketStellModel * marketModel = [SupermarketStellModel yy_modelWithJSON:value];
            if (marketModel.code == 0) {
                [self.stellDataSource addObjectsFromArray:marketModel.data.infos];
                [self noDataShowView];
                [self.myTableView reloadData];
            }
            return marketModel;
        }];
    }];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.stellDataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SupermarketStellModel_Infos * inforModel  = self.stellDataSource[indexPath.row];
    SupermarketStellTableViewCell * cell = [SupermarketStellTableViewCell cellWithTableView:tableView];
    cell.stell_inforModel = inforModel;
    cell.Block_InStore = ^{
        if (self.delegate && [self.delegate respondsToSelector:@selector(chickeStore:theStoreType:)]) {
            [self.delegate chickeStore:inforModel theStoreType:StoreOrGoodsInfor_Store];
        }
    };
    return cell;
}

#pragma mark ----- GoodsTopDelegate
-(void)selectTopViewButTag:(NSInteger)tag_num{
    if (screenView || screeningView) {
        [screeningView dissmiss];
        [screenView dissmiss];
    }
    switch (tag_num) {
        case 180:{
            screeningView = [[AdressAndSortingView alloc] initWithFrame:CGRectMake(0, kHeightProportion_In_iPhoneTriploid(35), KScreenWidth, KScreenHeight - kHeightProportion_In_iPhoneTriploid(35)) theTableListType:ListType_AllAdress inSuperView:self.selfView];
        }
            break;
        case 181:{
            screeningView = [[AdressAndSortingView alloc] initWithFrame:CGRectMake(0, kHeightProportion_In_iPhoneTriploid(35), KScreenWidth, KScreenHeight - kHeightProportion_In_iPhoneTriploid(35)) theTableListType:ListType_Comprehensive inSuperView:self.selfView];
        }
            break;
        case 182:{
            CGFloat sc_height;
            if (iPhoneX) {
                sc_height = KScreenHeight - kHeightProportion_In_iPhoneTriploid(35) - 88 - kHeightProportion_In_iPhoneTriploid(34);
            }else{
                sc_height = KScreenHeight - kHeightProportion_In_iPhoneTriploid(35) - 64;
            }
            screenView = [[ScreeningView alloc] initWithFrame:CGRectMake(0, kHeightProportion_In_iPhoneTriploid(35), KScreenWidth, sc_height) inSuperView:self.selfView];

        }
            break;
            
        default:
            break;
    }
}
//
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SupermarketStellModel_Infos * inforModel  = self.stellDataSource[indexPath.row];
    if (self.delegate && [self.delegate respondsToSelector:@selector(chickeStore:theStoreType:)]) {
        [self.delegate chickeStore:inforModel theStoreType:StoreOrGoodsInfor_Infor];
    }
}
@end
